//---------------------------
// Includes
//---------------------------
#include "../StdAfx.h"
#include "Glock.h"
#include "NxActor.h"
#include "NxShape.h"
#include "../Sphere.h"
#include "../Level.h"
#include "../CubePhysX.h"
#include "../Character.h"
#include "../DX10ObjMesh.h"
#include "../DX10ObjMesh.h"
#include "../GraphCamera.h"

Glock::Glock(Level* pLevel):
Weapon(pLevel)
{
	
}
Glock::~Glock()
{
	// nothing to destroGlock
}

void Glock::Tick(const InputState&  dTime)
{
	if(m_Mesh != 0)
	{

		//Wapen plaatsen//
		/////////////////
		float radialen = -1.570796327f;
		float xrot = 0;
		D3DXVECTOR3 tempvec;
		D3DXVECTOR4 test;
		D3DXMATRIX temprot;
		D3DXMATRIX tempWorld;
		m_Mesh->ResetWorldMatrix();
		//m_Character->GetWorldMatrix();

		tempvec = m_Character->GetCamera()->GetLook();
		temprot = m_Character->GetCamera()->GetView();

		D3DXMatrixInverse(&temprot, 0, &temprot);


		m_Mesh->Rotate(D3DXVECTOR3(radialen,-radialen,0));
		m_Mesh->Translate(D3DXVECTOR3(0.3f,-0.3f,1));
		tempWorld = m_Mesh->GetWorldMatrix();
		tempWorld *= temprot;

		m_Mesh->SetWorldMatrix(tempWorld);

		m_Mesh->Translate(tempvec);
		//End Weapon///////////
		/////////////

		if(dTime.IsLeftMouseButtonDown())
			Fire();
	}
}
void Glock::SetCharacter(Character* character)
{
	m_Character = character;
}
void Glock::Fire()
{
	NxRaycastHit hit;
	NxRay GunRay;
	NxVec3 GunDir(m_Character->GetCamera()->GetLook().x, m_Character->GetCamera()->GetLook().y, m_Character->GetCamera()->GetLook().z);
	NxVec3 GunOrig(m_Character->GetPosition().x,m_Character->GetPosition().y, m_Character->GetPosition().z);
	//Eventuele randomheid aan toevoegen
	GunRay.dir = GunDir;
	GunRay.dir.normalize();
	GunRay.orig = GunOrig;

	NxShape* HitShape = 0;

	HitShape = m_pLevel->GetPhysX()->GetClosestShape(GunRay, 1000);

	/*if(HitShape->getActor().userData != NULL)
	{
		LevelElement* temp = (LevelElement*)HitShape->getActor().userData;
		temp->OnGunHit();
	}*/
}

